//苹果升级（wgt文件）

var oldVersion = null,
	newVersion = null,
	downloadUrl = '';

function plusReady() {
	
	// 获取本地应用资源版本号
	plus.runtime.getProperty(plus.runtime.appid, function(inf) {
		oldVersion = inf.version.replace(/\./g, '');
		checkUpdate();
		
	});
}
if(window.plus) {
	plusReady();
} else {
	document.addEventListener('plusready', plusReady, false); 
}

function checkUpdate() {
	Get(SERVER_URL + "&c=Public&a=checkVersion", {}, function(data) {
		if(data.status) {
			downloadUrl = data.info.url;
			newVersion = data.info.version.replace(/\./g, '');
			if(newVersion > oldVersion) {
				//alert(newVersion+'/'+oldVersion)
				downWgt(downloadUrl)
			}
		}
	})
}

//从服务器下载应用资源包（wgt文件）
function downWgt(wgtUrl) {
	plus.downloader.createDownload(wgtUrl, {
		filename: "_doc/update/"
	}, function(d, status) {
		if(status == 200) {
			console.log("下载wgt成功：" + d.filename);
			//alert(d.filename); 
			installWgt(d.filename); // 安装wgt包
		} else {
			console.log("下载wgt失败！");
		}
	}).start();
}

// 更新应用资源
function installWgt(path) {
	plus.runtime.install(path, {}, function() {
		console.log("安装wgt文件成功！");
		plus.nativeUI.toast('更新成功')
		//alert('安装wgt文件成功！')
	}, function(e) {
		console.log("安装wgt文件失败[" + e.code + "]：" + e.message);
	});
}